package com.guodefu.sensitive.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.guodefu.sensitive.annotation.Sensitive;

public class SensitiveAnnotationIntrospector extends JacksonAnnotationIntrospector {

    private final SensitiveStrategyContext strategyContext;

    public SensitiveAnnotationIntrospector(SensitiveStrategyContext strategyContext, ObjectMapper objectMapper) {
        this.strategyContext = strategyContext;
        objectMapper.setAnnotationIntrospector(this);
    }

    @Override
    public Object findSerializer(Annotated a) {
        Sensitive sensitive = a.getAnnotation(Sensitive.class);
        if (sensitive != null) {
            return new SensitiveSerializer(sensitive, strategyContext);
        }
        return super.findSerializer(a);
    }
}
